import { api } from '@/utils/request'
import type { 
  ApiResponse, 
  Train, 
  Station, 
  CreateTrainRequest,
  Carriage,
  Seat,
  CreateCarriageRequest,
  CreateSeatRequest,
  User,
  Order,
  Ticket,
  TicketPurchaseRequest,
  LoginRequest,
  RefundTicketRequest,
  RescheduleTicketRequest,
  Payment,
  TransferRoute,
  TransferTicketPurchaseRequest
} from '@/types/train'

export const trainApi = {
  // 获取所有车次
  getAllTrains(): Promise<ApiResponse<Train[]>> {
    return api.get('/trains')
  },

  // 根据ID获取车次
  getTrainById(id: number): Promise<ApiResponse<Train>> {
    return api.get(`/trains/${id}`)
  },

  // 根据车次号获取车次
  getTrainByNumber(trainNumber: string): Promise<ApiResponse<Train>> {
    return api.get(`/trains/number/${trainNumber}`)
  },

  // 搜索车次
  searchTrains(startStationId: number, endStationId: number, travelDate: string): Promise<ApiResponse<Train[]>> {
    return api.get('/trains/search', {
      params: {
        startStationId,
        endStationId,
        travelDate
      }
    })
  },

  // 创建车次
  createTrain(trainData: CreateTrainRequest): Promise<ApiResponse<Train>> {
    return api.post('/trains', trainData)
  },

  // 更新车次
  updateTrain(id: number, trainData: CreateTrainRequest): Promise<ApiResponse<Train>> {
    return api.put(`/trains/${id}`, trainData)
  },

  // 取消车次
  cancelTrain(id: number): Promise<ApiResponse<void>> {
    return api.put(`/trains/${id}/cancel`)
  },

  // 删除车次
  deleteTrain(id: number): Promise<ApiResponse<void>> {
    return api.delete(`/trains/${id}`)
  },

  // 搜索换乘路线
  searchTransferRoutes(startStationId: number, endStationId: number, travelDate: string, maxTransferWait?: number): Promise<ApiResponse<TransferRoute[]>> {
    return api.get('/trains/transfer', {
      params: {
        startStationId,
        endStationId,
        travelDate,
        maxTransferWait: maxTransferWait || 120
      }
    })
  }
}

export const stationApi = {
  // 获取所有车站
  getAllStations(): Promise<ApiResponse<Station[]>> {
    return api.get('/stations')
  },

  // 根据ID获取车站
  getStationById(id: number): Promise<ApiResponse<Station>> {
    return api.get(`/stations/${id}`)
  },

  // 根据车站代码获取车站
  getStationByCode(stationCode: string): Promise<ApiResponse<Station>> {
    return api.get(`/stations/code/${stationCode}`)
  },

  // 创建车站
  createStation(stationData: Omit<Station, 'id' | 'createdAt'>): Promise<ApiResponse<Station>> {
    return api.post('/stations', stationData)
  },

  // 更新车站
  updateStation(id: number, stationData: Omit<Station, 'id' | 'createdAt'>): Promise<ApiResponse<Station>> {
    return api.put(`/stations/${id}`, stationData)
  },

  // 删除车站
  deleteStation(id: number): Promise<ApiResponse<void>> {
    return api.delete(`/stations/${id}`)
  }
}

export const carriageApi = {
  // 获取所有车厢列表
  getAllCarriages(): Promise<ApiResponse<Carriage[]>> {
    return api.get('/carriages')
  },

  // 根据车次ID获取车厢列表
  getCarriagesByTrainId(trainId: number): Promise<ApiResponse<Carriage[]>> {
    return api.get(`/carriages/train/${trainId}`)
  },

  // 根据ID获取车厢
  getCarriageById(id: number): Promise<ApiResponse<Carriage>> {
    return api.get(`/carriages/${id}`)
  },

  // 创建车厢
  createCarriage(carriageData: CreateCarriageRequest): Promise<ApiResponse<Carriage>> {
    return api.post('/carriages', carriageData)
  },

  // 更新车厢
  updateCarriage(id: number, carriageData: CreateCarriageRequest): Promise<ApiResponse<Carriage>> {
    return api.put(`/carriages/${id}`, carriageData)
  },

  // 删除车厢
  deleteCarriage(id: number): Promise<ApiResponse<void>> {
    return api.delete(`/carriages/${id}`)
  }
}

export const seatApi = {
  // 根据车厢ID获取座位列表
  getSeatsByCarriageId(carriageId: number): Promise<ApiResponse<Seat[]>> {
    return api.get(`/seats/carriage/${carriageId}`)
  },

  // 根据车次ID获取所有座位
  getSeatsByTrainId(trainId: number): Promise<ApiResponse<Seat[]>> {
    return api.get(`/seats/train/${trainId}`)
  },

  // 根据车次ID获取可用座位
  getAvailableSeatsByTrainId(trainId: number): Promise<ApiResponse<Seat[]>> {
    return api.get(`/seats/train/${trainId}/available`)
  },

  // 根据车厢ID获取可用座位
  getAvailableSeatsByCarriageId(carriageId: number): Promise<ApiResponse<Seat[]>> {
    return api.get(`/seats/carriage/${carriageId}/available`)
  },

  // 根据ID获取座位
  getSeatById(id: number): Promise<ApiResponse<Seat>> {
    return api.get(`/seats/${id}`)
  },

  // 创建座位
  createSeat(seatData: CreateSeatRequest): Promise<ApiResponse<Seat>> {
    return api.post('/seats', seatData)
  },

  // 更新座位
  updateSeat(id: number, seatData: CreateSeatRequest): Promise<ApiResponse<Seat>> {
    return api.put(`/seats/${id}`, seatData)
  },

  // 切换座位可用状态
  toggleSeatAvailability(id: number): Promise<ApiResponse<void>> {
    return api.put(`/seats/${id}/toggle`)
  },

  // 删除座位
  deleteSeat(id: number): Promise<ApiResponse<void>> {
    return api.delete(`/seats/${id}`)
  }
}

export const userApi = {
  // 获取所有用户
  getAllUsers(): Promise<ApiResponse<User[]>> {
    return api.get('/users')
  },

  // 根据ID获取用户
  getUserById(id: number): Promise<ApiResponse<User>> {
    return api.get(`/users/${id}`)
  },

  // 创建用户
  createUser(userData: Omit<User, 'id' | 'createdAt' | 'updatedAt'>): Promise<ApiResponse<User>> {
    return api.post('/users', userData)
  },

  // 更新用户
  updateUser(id: number, userData: Omit<User, 'id' | 'createdAt' | 'updatedAt'>): Promise<ApiResponse<User>> {
    return api.put(`/users/${id}`, userData)
  },

  // 删除用户
  deleteUser(id: number): Promise<ApiResponse<void>> {
    return api.delete(`/users/${id}`)
  },

  // 用户登录
  login(loginData: LoginRequest): Promise<ApiResponse<User>> {
    return api.post('/users/login', loginData)
  }
}

export const ticketApi = {
  // 购买票务
  purchaseTickets(purchaseData: TicketPurchaseRequest): Promise<ApiResponse<Order>> {
    return api.post('/tickets/purchase', purchaseData)
  },

  // 获取所有订单列表（管理员功能）
  getAllOrders(): Promise<ApiResponse<Order[]>> {
    return api.get('/tickets/admin/orders')
  },

  // 根据用户ID获取订单列表
  getOrdersByUserId(userId: number): Promise<ApiResponse<Order[]>> {
    return api.get(`/tickets/orders/user/${userId}`)
  },

  // 根据ID获取订单
  getOrderById(orderId: number): Promise<ApiResponse<Order>> {
    return api.get(`/tickets/orders/${orderId}`)
  },

  // 根据用户ID获取票务列表
  getTicketsByUserId(userId: number): Promise<ApiResponse<Ticket[]>> {
    return api.get(`/tickets/user/${userId}`)
  },

  // 取消订单
  cancelOrder(orderId: number): Promise<ApiResponse<void>> {
    return api.put(`/tickets/orders/${orderId}/cancel`)
  },

  // 删除订单
  deleteOrder(orderId: number): Promise<ApiResponse<void>> {
    return api.delete(`/tickets/orders/${orderId}`)
  },

  // 退票
  refundTicket(refundData: RefundTicketRequest): Promise<ApiResponse<void>> {
    return api.post('/tickets/refund', refundData)
  },

  // 改签
  rescheduleTicket(rescheduleData: RescheduleTicketRequest): Promise<ApiResponse<Order>> {
    return api.post('/tickets/reschedule', rescheduleData)
  },

  // 购买换乘票务
  purchaseTransferTickets(purchaseData: TransferTicketPurchaseRequest): Promise<ApiResponse<Order>> {
    return api.post('/tickets/transfer/purchase', purchaseData)
  }
}

export const paymentApi = {
  // 处理支付
  processPayment(orderId: number, paymentMethod: string): Promise<ApiResponse<Payment>> {
    return api.post('/payments/process', null, {
      params: {
        orderId,
        paymentMethod
      }
    })
  },

  // 根据订单ID获取支付记录
  getPaymentsByOrderId(orderId: number): Promise<ApiResponse<Payment[]>> {
    return api.get(`/payments/order/${orderId}`)
  }
}